package oleg;

/**
 * Copyright 2008 Oleg Frenkel
 *
 * This file is part of PerfectSearch.
 *
 * PerfectSearch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PerfectSearch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PerfectSearch.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.*;
import javax.microedition.pim.*;

public class ContactUtils
{
  int errCounter;

  public Vector getValuesAsVector(Contact curCont, int field) {
    return getValuesAsVector(curCont, field, null);
  }

  public Vector getValuesAsVector(Contact curCont, int field, Vector attrs) {
    Vector values = new Vector();
    if (!curCont.getPIMList().isSupportedField(field)) {
      return values;
    }

    errCounter = 2;
    int fieldDataType = curCont.getPIMList().getFieldDataType(field);
    switch(fieldDataType) {
      case Contact.INT:
        errCounter = 3;
        for (int i = 0; i < curCont.countValues(field); i++) {
          int curFieldAttrs = curCont.getAttributes(field, i);
          String curIntValue = "" + curCont.getInt(field, i);
          if ((curFieldAttrs & Contact.ATTR_PREFERRED) != 0) {
            values.insertElementAt(curIntValue.toUpperCase(), 0);
          } else {
            values.addElement(curIntValue.toUpperCase());
          }

          if (attrs != null) {
            attrs.addElement(getAttrString(curFieldAttrs));
          }
        }
        break;
      case Contact.STRING:
        errCounter = 3;
        for (int i = 0; i < curCont.countValues(field); i++) {
          int curFieldAttrs = curCont.getAttributes(field, i);
          String curStrValue = curCont.getString(field, i);
          if (curStrValue != null) {
            if ((curFieldAttrs & Contact.ATTR_PREFERRED) != 0) {
              values.insertElementAt(curStrValue.toUpperCase(), 0);
            } else {
              values.addElement(curStrValue.toUpperCase());
            }

            if (attrs != null) {
              attrs.addElement(getAttrString(curFieldAttrs));
            }
          }
        }
        break;
      case Contact.STRING_ARRAY:
        errCounter = 3;
        for (int i = 0; i < curCont.countValues(field); i++) {
          errCounter = 4;
          int curFieldAttrs = curCont.getAttributes(field, i);
          String[] curArrStrValue = curCont.getStringArray(field, i);
          if (curArrStrValue == null) {
            continue;
          }

          for (int j = 0; j < curArrStrValue.length; j++) {
            errCounter = 5;
            if (curArrStrValue[j] != null) {
              if ((curFieldAttrs & Contact.ATTR_PREFERRED) != 0) {
                values.insertElementAt(curArrStrValue[j].toUpperCase(), j);
              } else {
                values.addElement(curArrStrValue[j].toUpperCase());
              }

              if (attrs != null) {
                attrs.addElement(getAttrString(curFieldAttrs));
              }
            }
          }
          errCounter = 3;
        }
        break;
    }

    return values;
  }

  public String getAttrString(int attr) {
    if ((attr & Contact.ATTR_FAX) != 0) {
      return "(f)";
    }

    if ((attr & Contact.ATTR_HOME) != 0) {
      return "(h)";
    }

    if ((attr & Contact.ATTR_MOBILE) != 0) {
      return "(c)";
    }

    if ((attr & Contact.ATTR_OTHER) != 0) {
      return "(o)";
    }

    if ((attr & Contact.ATTR_PAGER) != 0) {
      return "(p)";
    }

    if ((attr & Contact.ATTR_SMS) != 0) {
      return "(s)";
    }

    if ((attr & Contact.ATTR_WORK) != 0) {
      return "(w)";
    }

    return "(g)";
  }
}
